using UnityEngine;
using System.Collections;

public class CsharpCamControl : MonoBehaviour
{
		public CsharpButtonInterface csharpButtonInterface;
		public Vector3 center;
		public float radius = 3.0f;
		private float angle = 80;
		private float angleSpeed;
		public float maxHeight = 3.0f;
		private AnimationCurve crvHeight;
		public float minHeight = 0.5f;
		private AnimationCurve crvLower;
		private float heightRate;
		private Vector3 posMove;
		private bool  disableOtherClicks = false;

		void Start ()
		{
				heightRate = (maxHeight + minHeight) * 0.5f;
				crvHeight = new AnimationCurve (new Keyframe (minHeight, 1), new Keyframe (maxHeight - heightRate, 1), new Keyframe (maxHeight, 0));
				crvLower = new AnimationCurve (new Keyframe (minHeight, 0), new Keyframe (minHeight + heightRate, 1), new Keyframe (maxHeight, 1));
	
		}

		void Update ()
		{
				float evaHeight = crvHeight.Evaluate (transform.position.y);
				float evaLower = crvLower.Evaluate (transform.position.y);

				if (Input.GetMouseButton (0) && csharpButtonInterface.CanIClick ()) {
						disableOtherClicks = true;

						if (Input.GetAxis ("Mouse Y") < 0) {
								posMove.y -= Input.GetAxis ("Mouse Y") * evaHeight;	
						} else {
								posMove.y -= Input.GetAxis ("Mouse Y") * evaLower;
						}
		
						angleSpeed -= Mathf.Abs (Mathf.Pow (Input.GetAxis ("Mouse X"), 1.0f)) * Mathf.Sign (Input.GetAxis ("Mouse X")) * 50.0f;
				}
	
				if (Input.GetMouseButtonUp (0)) {
						disableOtherClicks = false;
				}

				angleSpeed = Mathf.Lerp (angleSpeed, 0, Time.deltaTime * 5.0f);
				angle += angleSpeed * Time.deltaTime;
	
				float calRadius = Mathf.Lerp (radius * .5f, radius, evaHeight);
		
				Vector2 desiredHorizontalPosition;
				desiredHorizontalPosition.x = Mathf.Cos (angle * Mathf.Deg2Rad) * calRadius;
				desiredHorizontalPosition.y = Mathf.Sin (angle * Mathf.Deg2Rad) * calRadius;
	
//		transform.position = new Vector3 (desiredHorizontalPosition.x, transform.position.y, desiredHorizontalPosition.y);
////		transform.position.z = desiredHorizontalPosition.y;
//					
//		posMove = Vector3.Lerp (posMove, Vector3.zero, Time.deltaTime * 5.0f);
//	
//		if (posMove.y > 0 && transform.position.y > maxHeight - heightRate) {
//			posMove.y = Mathf.Lerp (posMove.y, 0, 1 - evaHeight);
//		}
//	
//		transform.Translate (posMove * Time.deltaTime, Space.World);
//		transform.position = transform.up * (Mathf.Clamp (transform.position.y, minHeight, maxHeight));
//	
//		transform.LookAt (center);
		}

		public bool CanIClick ()
		{
				return !disableOtherClicks;
		}
}